package com.ruoyi.security.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.YSResult;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.security.config.YSConfig;
import com.ruoyi.security.domain.AfDeviceCamera;
import com.ruoyi.security.mapper.AfDeviceCameraMapper;
import com.ruoyi.security.service.IAfDeviceCameraService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 摄像头Service业务层处理
 *
 * @author cjie
 * @date 2022-07-13
 */
@Service
public class AfDeviceCameraServiceImpl implements IAfDeviceCameraService {
    @Autowired
    private AfDeviceCameraMapper afDeviceCameraMapper;

    /**
     * 查询摄像头
     *
     * @param id 摄像头ID
     * @return 摄像头
     */
    @Override
    public AfDeviceCamera selectAfDeviceCameraById(String id) {
        return afDeviceCameraMapper.selectAfDeviceCameraById(id);
    }

    /**
     * 查询摄像头列表
     *
     * @param afDeviceCamera 摄像头
     * @return 摄像头
     */
    @Override
    public List<AfDeviceCamera> selectAfDeviceCameraList(AfDeviceCamera afDeviceCamera) {
        return afDeviceCameraMapper.selectAfDeviceCameraList(afDeviceCamera);
    }

    /**
     * 新增摄像头
     *
     * @param afDeviceCamera 摄像头
     * @return 结果
     */
    @Override
    public int insertAfDeviceCamera(AfDeviceCamera afDeviceCamera) {
        return afDeviceCameraMapper.insertAfDeviceCamera(afDeviceCamera);
    }

    /**
     * 修改摄像头
     *
     * @param afDeviceCamera 摄像头
     * @return 结果
     */
    @Override
    public int updateAfDeviceCamera(AfDeviceCamera afDeviceCamera) {
        return afDeviceCameraMapper.updateAfDeviceCamera(afDeviceCamera);
    }

    /**
     * 批量删除摄像头
     *
     * @param ids 需要删除的摄像头ID
     * @return 结果
     */
    @Override
    public int deleteAfDeviceCameraByIds(String[] ids) {
        return afDeviceCameraMapper.deleteAfDeviceCameraByIds(ids);
    }

    /**
     * 删除摄像头信息
     *
     * @param id 摄像头ID
     * @return 结果
     */
    @Override
    public int deleteAfDeviceCameraById(String id) {
        return afDeviceCameraMapper.deleteAfDeviceCameraById(id);
    }

    @Override
    public int getOnlineList() {
        int result = 0;
        String token = redisService.getCacheObject(YSConfig.appKey);
        List<AfDeviceCamera> cameraList = getCameraList(token);

        //批量插入
        List<AfDeviceCamera> cameraListTmps = new ArrayList<>();
        for(AfDeviceCamera cameraId:cameraList){
            AfDeviceCamera cameraTmp = afDeviceCameraMapper.selectAfDeviceCameraById(cameraId.getId());
            if(cameraTmp == null){
                cameraListTmps.add(cameraTmp);
            }else {
                //todo 更新camera列表
            }
        }
        //afDeviceCameraMapper.deleteAfDeviceCameraByIds(arr);
        //批量插入,拼接
        //cameraList
        concatPCUrl(cameraListTmps);
        result = afDeviceCameraMapper.insertBatch(cameraListTmps);

        return result;
    }

    private List<AfDeviceCamera> getCameraList(String token){
        //获取线上数据
        String url = "https://open.ys7.com/api/lapp/camera/list" +"?accessToken="+ token +"&pageStart=0&pageSize=10";
        HttpRequest httpRequest= HttpUtil.createPost(url).header("content-type","application/x-www-form-urlencoded");
        String resultStr = httpRequest.execute().body();
        JSONObject jsonObject = JSONUtil.parseObj(resultStr);

        JSONArray array = JSONUtil.parseArray(jsonObject.get("data"));

        List<AfDeviceCamera> cameraList = JSONUtil.toList(array, AfDeviceCamera.class);

        return cameraList;
    }

    private void concatPCUrl(List<AfDeviceCamera> cameraList){
        String pcBaseUrl = "ezopen://open.ys7.com/";
        //sb.
        cameraList.stream().forEach(item->{
            StringBuilder sb = new StringBuilder();
            sb.append(pcBaseUrl).append(item.getDeviceSerial()).append("/").append(item.getChannelNo()).append(".live");
            item.setPcUrl(sb.toString());
        });
    }

    @Autowired
    private RedisService redisService;
    public String getAccessToken(){
        String token = redisService.getCacheObject(YSConfig.appKey);
        return token;
    }


}
